<?php


namespace app\admin\model;


use think\facade\Db;
use think\Model;
use think\model\concern\SoftDelete;

class Applies extends Model
{
    use SoftDelete;
    protected $name = 'applies';
    ## 章
    public static $chapter = array('公司章', '财务章', '合同章', '其他');
    ## 车
    public static $car = array('轿车-宝马-1.5T', 'SUV-哈弗H6-1.5T', '商务车-别克GL8-1.5T', '其他');
    ##money 钱
    public static $money = array('现金-1000', '转账-1000', '其他');
    ##采购
    public static $purchase = array('记号笔-2', '茶叶-3', '电脑配件-4', '其他');


    public static function checkDetail($table,$id){
        $model = self::with(['admin' => ['AuthGroup'], 'typeName'])->find($id);
        $item = empty($model) ? array() : $model->toArray();
        $res = self::searchTable($table, $id);
        $item['username'] = $item['admin']['nickname'] ?? $item['admin']['username'];
        $item['status_table'] = $res['status'] ?? 1;
        $item['type_name'] = $item['typeName']['name'] ?? "休假";
        $item['res'] = $res;
        return $item;
    }

    public static function searchTable($table, $table_id)
    {
        $db = Db::name($table)->find($table_id);
        switch ($table){
            case "vacations":
                $db['qb'] = 1;
                $db['shiyou'] = $db['remark'] ?? '';
                break;
            case "applies":
                $db['qb'] = 2;
                $db['shiyou'] = $db['content'] ?? '';
                break;
        }
        return $db;
    }



    public function appliyType(){
        return $this->belongsTo(ApplyType::class,'type_id');
    }

    public function admin(){
        return $this->belongsTo(Admin::class,'admin_id');
    }

    public function typeName(){
        return $this->belongsTo(ApplyType::class,'type_id');
    }


    public function money(){
        return $this->hasOne(Money::class,'applies_id');
    }
    public function che(){
        return $this->hasMany(Che::class,'applies_id');
    }
    public function cg(){
        return $this->hasMany(Caigou::class,'applies_id');
    }
    public function zhang(){
        return $this->belongsTo(Zhang::class,'applies_id');
    }

}